package com.sc.utils;

import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.sc.entity.params.MaterialParam;
import com.sc.entity.params.ProgramParam;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class HKUtils {
    public static String getCameraPreviewURL(String cameraIndexCode) {

        /**
         * STEP1：设置平台参数，根据实际情况,设置host appkey appsecret 三个参数.
         */
        /*ArtemisConfig.host = "10.80.128.21:443"; // 平台的ip端口
        ArtemisConfig.appKey = "23851677";  // 密钥appkey
        ArtemisConfig.appSecret = "catuYvg2lLh4979gpWIX";// 密钥appSecret
*/
        /**
         * STEP2：设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = "/artemis";

        /**
         * STEP3：设置接口的URI地址
         */
        final String previewURLsApi = ARTEMIS_PATH + "/api/video/v2/cameras/previewURLs";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };

        /**
         * STEP4：设置参数提交方式
         */
        String contentType = "application/json";

        /**
         * STEP5：组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        //"cc597c58a4e640938520567821b3985a"
        jsonBody.put("cameraIndexCode", cameraIndexCode);
        jsonBody.put("streamType", 0);
        //jsonBody.put("protocol", "hls");
        jsonBody.put("protocol", "ws");
        jsonBody.put("transmode", 1);
        jsonBody.put("expand", "streamform=ps");
        String body = jsonBody.toJSONString();
        /**
         * STEP6：调用接口
         */

        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType , null);// post请求application/json类型参数

        JSONObject json=JSONObject.parseObject(result);
        JSONObject data=(JSONObject)json.get("data");
        String url=(String)data.get("url");

        return url;
    }


    public static String pageList(int pageNum,int pageSize) {

        /**
         * STEP1：设置平台参数，根据实际情况,设置host appkey appsecret 三个参数.
         */
//        ArtemisConfig.host = "10.80.128.21:443"; // 平台的ip端口
//        ArtemisConfig.appKey = "23851677";  // 密钥appkey
//        ArtemisConfig.appSecret = "catuYvg2lLh4979gpWIX";// 密钥appSecret

        /**
         * STEP2：设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = "/artemis";

        /**
         * STEP3：设置接口的URI地址
         */
        final String previewURLsApi = ARTEMIS_PATH + "/api/resource/v2/camera/search";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };

        /**
         * STEP4：设置参数提交方式
         */
        String contentType = "application/json";

        /**
         * STEP5：组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("pageNo", pageNum);
        jsonBody.put("pageSize", pageSize);
        /*
        jsonBody.put("protocol", "hls");
        jsonBody.put("transmode", 1);
        jsonBody.put("expand", "streamform=ps");*/
        String body = jsonBody.toJSONString();
        /**
         * STEP6：调用接口
         */
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType , null);// post请求application/json类型参数
        return result;
    }
TODO
    public static String getProgram(ProgramParam param, int pageNo, int pageSize) {
        /**
         * STEP2：设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = "/artemis";

        final String previewURLsApi = ARTEMIS_PATH + "/api/xpublish-web/v1/programService/programs";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };
        /**
         * STEP4：设置参数提交方式
         */
        String contentType = "application/json";

        /**
         * STEP5：组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("param", param);
        jsonBody.put("pageNo", pageNo);
        jsonBody.put("pageSize", pageSize);
        String body = jsonBody.toJSONString();

/**
         * STEP6：调用接口
         */
        log.info("调用海康参数：{},{},{},{},{}",path,body, ArtemisConfig.appKey,ArtemisConfig.host,ArtemisConfig.appSecret);
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType , null);// post请求application/json类型参数
        log.debug("调用海康返回值：{}",result);
        return result;
    }

    public static String getMaterialSingleDetail(Integer id) {
        /**
         * STEP2：设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = "/artemis";

        final String previewURLsApi = ARTEMIS_PATH + "/api/xpublish-web/v1/materialService/materialById";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };

        /**
         * STEP4：设置参数提交方式
         */
        String contentType = "application/json";

        /**
         * STEP5：组装请求参数
         */
        JSONObject jsonBody = new JSONObject();

        jsonBody.put("materialId", id);
        String body = jsonBody.toJSONString();
        /**
         * STEP6：调用接口
         */
        log.info("调用海康参数：{},{},{},{},{}",path,body, ArtemisConfig.appKey,ArtemisConfig.host,ArtemisConfig.appSecret);
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType , null);// post请求application/json类型参数
        log.debug("调用海康返回值：{}",result);
        return result;
    }

    public static String getMaterials(MaterialParam param) {
        /**
         * STEP2：设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = "/artemis";

        final String previewURLsApi = ARTEMIS_PATH + "/api/xpublish-web/v1/materialService/materials";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };

         /**
         * STEP4：设置参数提交方式
         */
        String contentType = "application/json";

        /**
         * STEP5：组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("indexCodes", new ArrayList<>());
        jsonBody.put("pageNo", param.getPageNo());
        jsonBody.put("pageSize", param.getPageSize());

        String body = jsonBody.toJSONString();
        /**
         * STEP6：调用接口
         */
        log.info("调用海康参数：{},{},{},{},{}",path,body, ArtemisConfig.appKey,ArtemisConfig.host,ArtemisConfig.appSecret);
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType , null);// post请求application/json类型参数
        log.debug("调用海康返回值：{}",result);
        return result;
    }

    public static String getStatus(List<String> indexCodes, int pageNum, int pageSize) {

        /**
         * STEP1：设置平台参数，根据实际情况,设置host appkey appsecret 三个参数.
         */
/*        ArtemisConfig.host = "10.80.128.21:443"; // 平台的ip端口
        ArtemisConfig.appKey = "23851677";  // 密钥appkey
        ArtemisConfig.appSecret = "catuYvg2lLh4979gpWIX";// 密钥appSecret*/

        /**
         * STEP2：设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = "/artemis";

        /**
         * STEP3：设置接口的URI地址
         */
//        final String previewURLsApi = ARTEMIS_PATH + "/api/nms/v1/online/camera/get";
//        final String previewURLsApi = ARTEMIS_PATH + "/api/nms/v1/online/encode_device/get";
        final String previewURLsApi = ARTEMIS_PATH + "/api/xpublish-web/v1/terminalService/terminals";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };

        /**
         * STEP4：设置参数提交方式
         */
        String contentType = "application/json";

        /**
         * STEP5：组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("indexCodes", indexCodes);
        jsonBody.put("pageNo", pageNum);
        jsonBody.put("pageSize", pageSize);
        /*
        jsonBody.put("pageSize", pageSize);

        jsonBody.put("protocol", "hls");
        jsonBody.put("transmode", 1);
        jsonBody.put("expand", "streamform=ps");*/
        String body = jsonBody.toJSONString();
        /**
         * STEP6：调用接口
         */
        log.info("调用海康参数：{},{},{},{},{}",path,body, ArtemisConfig.appKey,ArtemisConfig.host,ArtemisConfig.appSecret);
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType , null);// post请求application/json类型参数
        log.debug("调用海康返回值：{}",result);
        return result;
    }



    public static void main(String[] args) {
        /*String result = HKUtils.pageList(1,10);
        JSONObject json=JSONObject.parseObject(result);
        DeviceSearchDTO searh=JSONObject.parseObject(JSONObject.toJSONString(json.get("data")),DeviceSearchDTO.class);

        result = allList(searh.getTotal());
        json=JSONObject.parseObject(result);
        searh=JSONObject.parseObject(JSONObject.toJSONString(json.get("data")),DeviceSearchDTO.class);

        //System.out.println(result);
        String result2=getCameraPreviewURL("98cc1e3796f1473287ca1ca1b9952e1e");
        System.out.println("result结果示例: " + result2);*/

        //String s=getStatus(Arrays.asList("98cc1e3796f1473287ca1ca1b9952e1e"));
        //System.out.println(s);
    }
}
